﻿@using SalePromotion.Extensions
@using SalePromotion.Pagination

@model IPagedList

<div class="pagination">
    @{
        int adjacents = 5;
        int lpm1 = Model.TotalPages - 1; //last page minus 1
    }
   
    @if (Model.TotalPages > 1)
    {
        <ul>
            @if (Model.IsFirstPage)
            {
                <li class="prev disabled"><a href="#">&larr; Previous</a></li>
            }
            else
            {
                <li class="prev"><a href="@Html.PagedUrl(Model.PreviousPage)">&larr; Previous</a></li>
            }

            @if (Model.TotalPages < 7 + (adjacents * 2))
            {
                foreach (var page in Model.Pages)
                {
                    if (page == Model.CurrentPage)
                    {
                        <li class="active"><a href="#">@page</a></li>
                    }
                    else
                    {
                        <li><a href="@Html.PagedUrl(page)">@page</a></li>
                    }
                }
            }
            else if (Model.TotalPages > 5 + (adjacents * 2))
            {
            @* Enough pages to hide some *@
            @* Close to beginning; only hide later pages *@
            
                if (Model.CurrentPage < 1 + (adjacents * 2))
                {
                    for (var page = 1; page < 4 + (adjacents * 2); page++)
                    {
                        if (page == Model.CurrentPage)
                        {
                            <li class="active"><a href="#">@page</a></li>
                        }
                        else
                        {
                            <li><a href="@Html.PagedUrl(page)">@page</a></li>
                        }
                    }
                
                    <li class="disabled"><a href="#">…</a></li>
                    <li><a href="@Html.PagedUrl(Model.TotalPages - 1)">@(Model.TotalPages - 1)</a></li>
                    <li><a href="@Html.PagedUrl(Model.TotalPages)">@Model.TotalPages</a></li>
                }
                else if (Model.TotalPages - (adjacents * 2) > Model.CurrentPage && Model.CurrentPage > (adjacents * 2))
                {
                @* In middle; hide some front and some back *@
                    <li><a href="@Html.PagedUrl(1)">1</a></li>
                    <li><a href="@Html.PagedUrl(2)">2</a></li>
                    <li class="disabled"><a href="#">…</a></li>
            
                    for (var page = Model.CurrentPage - adjacents; page <= Model.CurrentPage + adjacents; page++)
                    {
                        if (page == Model.CurrentPage)
                        {
                            <li class="active"><a href="#">@page</a></li>
                        }
                        else
                        {
                            <li><a href="@Html.PagedUrl(page)">@page</a></li>
                        }
                    }
               
                    <li class="disabled"><a href="#">…</a></li>
                    <li><a href="@Html.PagedUrl(Model.TotalPages - 1)">@(Model.TotalPages - 1)</a></li>
                    <li><a href="@Html.PagedUrl(Model.TotalPages)">@Model.TotalPages</a></li>
                }
                else
                {
                @* close to end; only hide early pages *@
                    <li><a href="@Html.PagedUrl(1)">1</a></li>
                    <li><a href="@Html.PagedUrl(2)">2</a></li>
                    <li class="disabled"><a href="#">…</a></li>
            
                    for (var page = Model.TotalPages - (2 + (adjacents * 2)); page <= Model.TotalPages; page++)
                    {
                        if (page == Model.CurrentPage)
                        {
                            <li class="active"><a href="#">@page</a></li>
                        }
                        else
                        {
                            <li><a href="@Html.PagedUrl(page)">@page</a></li>
                        }
                    }
                }
            }

            @* Next button *@
            @if (Model.IsLastPage)
            {
                <li class="next disabled"><a href="#">Next &rarr;</a></li>        
            }
            else
            {
                <li class="next"><a href="@Html.PagedUrl(Model.NextPage)">Next &rarr;</a></li>
            }
        </ul>
  
    }
</div>


